package com.smart.campus.school.service.impl;

import java.util.List;
import java.util.Map;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import cn.hutool.crypto.SecureUtil;
import com.smart.campus.common.utils.DateUtils;
import com.smart.campus.school.wx.cp.api.StudentApi;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.smart.campus.school.mapper.StudentMapper;
import com.smart.campus.school.domain.Student;
import com.smart.campus.school.service.IStudentService;
import com.smart.campus.common.core.text.Convert;

/**
 * 学生信息Service业务层处理
 * 
 * @author Zhihe.Li
 * @date 2020-05-18
 */
@Service
public class StudentServiceImpl implements IStudentService 
{
    @Autowired
    private StudentMapper studentMapper;

    /**
     * 查询学生信息
     * 
     * @param id 学生信息ID
     * @return 学生信息
     */
    @Override
    public Student selectStudentById(Long id)
    {
        return studentMapper.selectStudentById(id);
    }

    /**
     * 查询学生信息列表
     * 
     * @param student 学生信息
     * @return 学生信息
     */
    @Override
    public List<Student> selectStudentList(Student student)
    {
        return studentMapper.selectStudentList(student);
    }

    /**
     * 新增学生信息
     * 
     * @param student 学生信息
     * @return 结果
     */
    @Override
    public int insertStudent(Student student)
    {
        //参数1为终端ID
        //参数2为数据中心ID
        Snowflake snowflake = IdUtil.createSnowflake(1, 1);

       /* String maxStudentNo = studentMapper.getLastStudentNo(student);
        if(StringUtils.isEmpty(maxStudentNo)){
            maxStudentNo = String.valueOf(snowflake.nextId());
        }else {
            maxStudentNo = String.format("%03d",Integer.valueOf(maxStudentNo.substring(maxStudentNo.length()-4,maxStudentNo.length()-1))+1);
        }
        student.setStudentNo(maxStudentNo);*/
        student.setStatus(1);
        student.setPassword(SecureUtil.md5("123456"));
        student.setCreateTime(DateUtils.getNowDate());
        int result = studentMapper.insertStudent(student);
       // StudentApi.createStudent(student);
        return result;
    }


    /**
     * 修改学生信息
     * 
     * @param student 学生信息
     * @return 结果
     */
    @Override
    public int updateStudent(Student student)
    {
        student.setUpdateTime(DateUtils.getNowDate());
        return studentMapper.updateStudent(student);
    }

    /**
     * 删除学生信息对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteStudentByIds(String ids)
    {
        return studentMapper.deleteStudentByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除学生信息信息
     * 
     * @param id 学生信息ID
     * @return 结果
     */
    @Override
    public int deleteStudentById(Long id)
    {
        return studentMapper.deleteStudentById(id);
    }

    /**
     * 通过班级id查询学生数量
     *
     * @param classIdList
     * @return
     */
    @Override
    public int getStudentCountByClassIdList(List<Long> classIdList) {
        return studentMapper.getStudentCountByClassIdList(classIdList);
    }

    /**
     * 通过班级id查询学生
     *
     * @param classIdList
     * @return
     */
    @Override
    public List<Student> getStudentByClassIdList(List<Long> classIdList) {
        return studentMapper.getStudentByClassIdList(classIdList);
    }

    /***
     * 按照年纪查询学生，并按照学号排序
     * @param student
     * @return
     */
    @Override
    public List<Student> selectStudentListOrderByStudentNo(Student student) {
        return studentMapper.selectStudentListOrderByStudentNo(student);
    }

    /**
     * 通过登录信息查询学生
     *
     * @param studentNo
     * @param password
     * @return 学生信息
     */
    @Override
    public Student selectStudentByLogin(String studentNo, String password) {
        password = SecureUtil.md5(password);
        return studentMapper.selectStudentByLogin(studentNo,password);

    }

    @Override
    public Student selectStudentByStudentNo(String studentNo) {
        return studentMapper.selectStudentByStudentNo(studentNo);
    }

    /**
     * 获取学生 老师 班级 年级数量
     *
     * @return
     */
    @Override
    public Map<String, Integer> getTotalCount() {
        return studentMapper.getTotalCount();
    }

    public static void main(String[] args) {
        System.out.println(SecureUtil.md5("123456"));
    }
}
